package com.foosun.core.controller;

import com.alibaba.fastjson.JSONObject;
import com.foosun.core.base.BaseAction;
import com.foosun.core.base.PageData;
import com.foosun.core.entity.GroupEnumType;
import com.foosun.core.entity.Resource;
import com.foosun.core.service.ResourceGroupService;
import com.foosun.core.service.ResourceService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by Nelin on 14-5-30.
 */
@Controller
@RequestMapping("/common/resource")
public class ResourceAction implements BaseAction<Resource> {

    @javax.annotation.Resource
    private ResourceService resourceService;

    @javax.annotation.Resource
    private ResourceGroupService resourceGroupService;

    @RequestMapping("/list")
    public String list(Model model, PageData<Resource> pageData, HttpServletRequest request) throws Exception {
        pageData.setWebMap(request.getParameterMap());
        pageData = resourceService.findList(pageData);
        model.addAttribute("pageData", pageData);
        model.addAttribute("groups", resourceGroupService.findAll());
        return "/common/resource/list";
    }

    @RequestMapping("/list.flex")
    @ResponseBody
    public PageData _list(Model model, PageData<Resource> pageData, HttpServletRequest request) throws Exception {
        pageData.setWebMap(request.getParameterMap());
        pageData = resourceService.findList(pageData);
        return pageData;
    }

    @RequestMapping("/new")
    public String form(Model model) throws Exception {
        model.addAttribute("groups", resourceGroupService.findAll());
        model.addAttribute("types", GroupEnumType.values());
        return "/common/resource/input";
    }


    @RequestMapping("/new.flex")
    @ResponseBody
    public JSONObject _form(Model model) throws Exception {
        JSONObject data = new JSONObject();
        data.put("groups", resourceGroupService.findAll());
        data.put("types", GroupEnumType.values());
        return data;
    }

    @RequestMapping("/edit/{id}")
    public String edit(Model model, @PathVariable("id") Integer id) throws Exception {
        Resource resource = resourceService.findById(id);
        model.addAttribute("entity", resource);
        model.addAttribute("groups", resourceGroupService.findAll());
        model.addAttribute("types", GroupEnumType.values());
        return "/common/resource/input";
    }

    @RequestMapping("/edit.flex")
    @ResponseBody
    public JSONObject _edit(Model model,Integer id) throws Exception {
        Resource resource = resourceService.findById(id);
        JSONObject data = new JSONObject();
        data.put("entity", resource);
        data.put("groups", resourceGroupService.findAll());
        data.put("types", GroupEnumType.values());
        return data;
    }


    @RequestMapping("/repeat/name")
    @ResponseBody
    public Boolean repeatName(Resource resource) throws Exception {
        List<Resource> resources = resourceService.findByProperty("name", resource.getName());
        if (resources != null && !resources.isEmpty()) {
            if (!resources.get(0).getId().equals(resource.getId())) {
                return false;
            }
        }
        return true;
    }

    @RequestMapping("/save")
    public String save(Model model, Resource resource) throws Exception {
        resourceService.save(resource);
        return "redirect:/common/resource/list";
    }

    @RequestMapping("/save.flex")
    @ResponseBody
    public Boolean _save(Resource resource) throws Exception {
        resourceService.save(resource);
        return true;
    }

    @RequestMapping("/delete/{id}")
    public String delete(Model model, @PathVariable("id") Integer id) throws Exception {
        resourceService.delete(id);
        return "redirect:/common/resource/list";
    }

    @RequestMapping("/delete.flex")
    @ResponseBody
    public Boolean delete(Integer id) throws Exception {
        resourceService.delete(id);
        return true;
    }

    @RequestMapping("/delete/all")
    public String deleteAll(Model model) throws Exception {
        resourceService.deleteAll();
        return "redirect:/common/resource/list";
    }

}
